import cn.kgc.pojo.Student;
import cn.kgc.service.StudentService;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Rock
 * @create 2020-02-21 14:11
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService service;

    @RequestMapping("/toSave")
    public String toSave() {
        return "student_save";
    }

    @RequestMapping("/queryByName")
    public String queryByName(Model model, String studentName, Integer pageNum, Integer pageSize) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 5;
        }
        PageInfo<Student> pageInfo = service.queryByStudentName(studentName, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        //用于查询条件回显
        model.addAttribute("studentName", studentName);
        return "stuList";
    }

    @RequestMapping("/querySyudentById")
    public String querySyudentById(Model model, Integer id) {
        Student student = service.queryStudentById(id);
        model.addAttribute("student", student);
        return "student_ditail";
    }

    @RequestMapping("/delStu")
    public String delStuById(Integer id) {
        service.delStuById(id);
        return "redirect:/student/queryByName";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Student student) {
        Integer count = service.save(student);
        if (count > 0) {
            return "yes";
        }
        return "no";
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(Integer id) {
        Integer count = service.delete(id);
        if (count > 0) {
            return "yes";
        }
        return "no";
    }
}
